package com.xiaoyun.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTabbedPane;
import java.awt.Insets;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.GroupLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
	private final Action action = new SwingAction();
	static public JTabbedPane tabbedPane;
	GetComponent getComponent;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		getComponent = new GetComponent();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 792, 499);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("系统设置");
		menuBar.add(mnNewMenu);

		JMenuItem menuItem_7 = new JMenuItem("切换主题");
		mnNewMenu.add(menuItem_7);

		JMenuItem mntmNewMenuItem = new JMenuItem("切换用户");
		mnNewMenu.add(mntmNewMenuItem);

		JMenuItem menuItem_4 = new JMenuItem("关于系统");
		mnNewMenu.add(menuItem_4);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("锁定系统");
		mnNewMenu.add(mntmNewMenuItem_1);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("退出系统");
		mnNewMenu.add(mntmNewMenuItem_2);

		JMenu mnNewMenu_1 = new JMenu("信息查询");
		menuBar.add(mnNewMenu_1);

		JMenuItem menuItem_1 = new JMenuItem("财务信息");
		mnNewMenu_1.add(menuItem_1);

		JMenuItem menuItem = new JMenuItem("库存信息");
		mnNewMenu_1.add(menuItem);

		JMenuItem mntmNewMenuItem_5 = new JMenuItem("商品信息");
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CmmodityMsgPanel cmmodityMsgPanel = getComponent.getCmmodityMsgPanel();
				int length = tabbedPane.getTabCount();
				for(int i=0;i<length;i++){
					if(tabbedPane.getTabComponentAt(i).equals(cmmodityMsgPanel)){
						tabbedPane.setSelectedComponent(cmmodityMsgPanel);
						return;
					}		
				}
				new addTab(cmmodityMsgPanel,"商品信息");
				tabbedPane.setSelectedComponent(cmmodityMsgPanel);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_5);

		JMenuItem mntmNewMenuItem_4 = new JMenuItem("员工信息");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EmployeeMsgPanel employeeMsgPanel = getComponent.getEmployeeMsgPanel();
				int length = tabbedPane.getTabCount();
				for(int i=0;i<length;i++){
					if(tabbedPane.getTabComponentAt(i).equals(employeeMsgPanel)){
						tabbedPane.setSelectedComponent(employeeMsgPanel);
						return;
					}		
				}
				new addTab(employeeMsgPanel,"员工信息");
				tabbedPane.setSelectedComponent(employeeMsgPanel);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_4);

		JMenuItem mntmNewMenuItem_18 = new JMenuItem("会员信息");
		mntmNewMenuItem_18.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MemberMsgPanel memberMsgPanel = getComponent.getMemberMsgPanel();
				int length = tabbedPane.getTabCount();
				for(int i=0;i<length;i++){
					if(tabbedPane.getTabComponentAt(i).equals(memberMsgPanel)){
						tabbedPane.setSelectedComponent(memberMsgPanel);
						return;
					}		
				}
				new addTab(memberMsgPanel,"会员信息");
				tabbedPane.setSelectedComponent(memberMsgPanel);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_18);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("供应商信息");
		mnNewMenu_1.add(mntmNewMenuItem_3);
		
		JMenuItem menuItem_8 = new JMenuItem("大客户信息");
		menuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PartnerMsgPanel partnerMsgPanel = getComponent.getPartnerMsgPanel();
				int length = tabbedPane.getTabCount();
				for(int i=0;i<length;i++){
					if(tabbedPane.getTabComponentAt(i).equals(partnerMsgPanel)){
						tabbedPane.setSelectedComponent(partnerMsgPanel);
						return;
					}		
				}
				new addTab(partnerMsgPanel,"大客户信息");
				tabbedPane.setSelectedComponent(partnerMsgPanel);
			}
		});
		mnNewMenu_1.add(menuItem_8);

		JMenu mnNewMenu_2 = new JMenu("经营分析");
		menuBar.add(mnNewMenu_2);

		JMenuItem mntmNewMenuItem_7 = new JMenuItem("入库分析");
		mnNewMenu_2.add(mntmNewMenuItem_7);

		JMenuItem mntmNewMenuItem_6 = new JMenuItem("出货分析");
		mnNewMenu_2.add(mntmNewMenuItem_6);

		JMenuItem mntmNewMenuItem_17 = new JMenuItem("利润分析");
		mnNewMenu_2.add(mntmNewMenuItem_17);

		JMenu mnNewMenu_3 = new JMenu("业务处理");
		menuBar.add(mnNewMenu_3);

		JMenuItem mntmNewMenuItem_8 = new JMenuItem("采购入库");
		mnNewMenu_3.add(mntmNewMenuItem_8);

		JMenuItem menuItem_2 = new JMenuItem("采购退货");
		mnNewMenu_3.add(menuItem_2);

		JMenuItem mntmNewMenuItem_10 = new JMenuItem("团购出库");
		mnNewMenu_3.add(mntmNewMenuItem_10);

		JMenuItem menuItem_3 = new JMenuItem("团购退货");
		mnNewMenu_3.add(menuItem_3);

		JMenu mnNewMenu_4 = new JMenu("零售会员");
		menuBar.add(mnNewMenu_4);

		JMenuItem mntmNewMenuItem_11 = new JMenuItem("POS开单");
		mnNewMenu_4.add(mntmNewMenuItem_11);

		JMenuItem mntmNewMenuItem_9 = new JMenuItem("POS管理");
		mnNewMenu_4.add(mntmNewMenuItem_9);

		JMenu menu = new JMenu("增值业务");
		menuBar.add(menu);

		JMenuItem menuItem_6 = new JMenuItem("促销活动");
		menu.add(menuItem_6);

		JMenuItem menuItem_5 = new JMenuItem("群发邮件");
		menu.add(menuItem_5);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0,
				Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		JToolBar toolBar = new JToolBar();
		GridBagConstraints gbc_toolBar = new GridBagConstraints();
		gbc_toolBar.anchor = GridBagConstraints.WEST;
		gbc_toolBar.insets = new Insets(0, 0, 5, 0);
		gbc_toolBar.gridx = 0;
		gbc_toolBar.gridy = 0;
		getContentPane().add(toolBar, gbc_toolBar);

		JLabel lblNewLabel = new JLabel("商品查询");
		toolBar.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("库存查询");
		toolBar.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("利润分析");
		toolBar.add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("退出系统");
		toolBar.add(lblNewLabel_3);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
		gbc_tabbedPane.insets = new Insets(0, 0, 5, 0);
		gbc_tabbedPane.fill = GridBagConstraints.BOTH;
		gbc_tabbedPane.gridx = 0;
		gbc_tabbedPane.gridy = 1;
		getContentPane().add(tabbedPane, gbc_tabbedPane);

		 new addTab(new JPanel(),"首页");
		
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.WEST;
		gbc_panel.fill = GridBagConstraints.VERTICAL;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 2;
		getContentPane().add(panel, gbc_panel);

		JLabel lblNewLabel_4 = new JLabel("服务器连接：");
		panel.add(lblNewLabel_4);

		JLabel lblNewLabel_5 = new JLabel("正常      ");
		panel.add(lblNewLabel_5);

		JLabel label = new JLabel("操作员：");
		panel.add(label);

		JLabel lblNewLabel_6 = new JLabel("张三");
		panel.add(lblNewLabel_6);

		JLabel lblNewLabel_7 = new JLabel("登入时间");
		panel.add(lblNewLabel_7);

		JLabel lblNewLabel_8 = new JLabel("1212");
		panel.add(lblNewLabel_8);

}
}
class SwingAction extends AbstractAction {
	public SwingAction() {
	putValue(NAME, "SwingAction");
	putValue(SHORT_DESCRIPTION, "Some short description");
	}

	public void actionPerformed(ActionEvent e) {
	}
}

class addTab implements MouseListener{  
	JPanel tabPanel ;
	JLabel labelTitle;
	JLabel labelClose;
   public addTab(JPanel panel,String str){  
	   tabPanel=new JPanel();
	   labelTitle = new JLabel(str);
	   labelClose = new JLabel(new ImageIcon(getClass().getResource("/res/close13.png")));
	   labelTitle.setHorizontalAlignment(JLabel.LEFT);//设置文字显示在最左边  
	   labelClose.setHorizontalAlignment(JLabel.RIGHT);// 设置文字显示在最右边  
	   tabPanel.add(labelTitle);  
	   tabPanel.add(labelClose);  
       MainFrame.tabbedPane.addTab("i",panel);  
       MainFrame.tabbedPane.setTabComponentAt(MainFrame.tabbedPane.indexOfComponent(panel),tabPanel);//实现这个功能的就这一条最重要的语句   
       labelClose.addMouseListener(this); 
   }  
 
   @Override  
 
   public void mouseClicked(MouseEvent arg0) {  
      // TODO Auto-generated method stub  
	   MainFrame.tabbedPane.remove(MainFrame.tabbedPane.indexOfTabComponent(tabPanel));  
 
   }  
 
   @Override  
 
   public void mouseEntered(MouseEvent e) {  
 
      // TODO Auto-generated method stub  
 
   }  
 
   @Override  
 
   public void mouseExited(MouseEvent arg0) {  
 
      // TODO Auto-generated method stub  
 
   }  
 
   @Override  
 
   public void mousePressed(MouseEvent arg0) {  
 
      // TODO Auto-generated method stub  
 
   }  
 
   @Override  
 
   public void mouseReleased(MouseEvent arg0) {  
 
      // TODO Auto-generated method stub  
 
   }  
 
}  